package Windows.ShowWindows;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;
import javax.swing.border.LineBorder;

import Tools.IMG;
import Windows.PicPanel;

public class ShowWindow extends JFrame implements MouseListener {
	private static WindowListener sw;
	public static void ConcealWindow(final JFrame f, int speed) {

		int AimX = f.getLocation().x + f.getSize().width / 2; // 目标X坐标
		int AimY = f.getLocation().y + f.getSize().height / 2; // 目标Y坐标
		int agox = f.getLocation().x;
		int agoy = f.getLocation().y;
		final int AimWidth = f.getSize().width; // 原始宽度
		int AimHeight = f.getSize().height; // 原始高度

		int n = (AimWidth < AimHeight ? AimWidth : AimHeight) / speed; // 预调整次数,
		// 平衡显示

		double SpeedX = (double) AimWidth / n; // 横速
		double SpeedY = (double) AimHeight / n; // 纵速

		while (f.getLocation().x < AimX || f.getLocation().y < AimY || f.getSize().width > f.getMinimumSize().width || f.getSize().height > f.getMinimumSize().height) {
			final int x = f.getLocation().x < AimX ? (int) (f.getLocation().x + (SpeedX += 8)) : AimX;
			final int y = f.getLocation().y < AimY ? (int) (f.getLocation().y + (SpeedY += 8)) : AimY;
			final int width = f.getSize().width > 0 ? (int) (f.getSize().width - 2 * SpeedX < 0 ? 0 : f.getSize().width - 2 * SpeedX) : 0;
			final int height = f.getSize().height > 0 ? (int) (f.getSize().height - 2 * SpeedY < 0 ? 0 : f.getSize().height - 2 * SpeedY) : 0;
			// java.awt.EventQueue.invokeLater(new Runnable() {
			//
			// public void run() {
			f.setBounds(x, y, width, height);
			f.repaint();
			double percent = (double) f.getSize().width / AimWidth;
			if (f.isUndecorated()) {
				com.sun.awt.AWTUtilities.setWindowOpacity(f, (float) f.getSize().width / AimWidth);// --窗口透明度设置
			}

			// }
			// });
			try {
				Thread.sleep(10);
			} catch (Exception e) {
			}

			// if (percent < 0.8d && f.isUndecorated())
			// f.setUndecorated(false); // 取消边框框架
		}
		f.setVisible(false);
		f.setBounds(agox, agoy, AimWidth, AimHeight);// 恢复到开始位置
		if (f.isUndecorated()) {
			com.sun.awt.AWTUtilities.setWindowOpacity(f, 1);// --窗口透明度设置
		}
	}
	public static void DisplayWindow(final JFrame f, int speed) {
		// System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width
		// System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height

		final int AimX = f.getLocation().x; // 原始X坐标
		final int AimY = f.getLocation().y; // 原始Y坐标
		final int AimWidth = f.getSize().width; // 原始宽度
		final int AimHeight = f.getSize().height; // 原始高度
		final Rectangle value = new Rectangle(AimX + AimWidth / 2, AimY + AimHeight / 2, 30, 30);
		//
		// boolean Undecorated = f.isUndecorated();// 备份框架
		speed += 2;
		int n = (AimWidth < AimHeight ? AimWidth : AimHeight) / speed;// 预调整次数
		// n=n>0?n:100;

		int spX = (int) ((double) AimWidth / n); // 左边
		int spY = (int) ((double) AimHeight / n); // 顶边
		int spWidth = 2 * spX;// 宽速
		int spHeight = 2 * spY;// 高速
		final int a = 0;// 加速度
		final Rectangle sped = new Rectangle(spX, spY, spWidth, spHeight);
		if (f.isUndecorated()) {
			com.sun.awt.AWTUtilities.setWindowOpacity(f, 0.01f);// --窗口透明度设置
		}
		f.setBounds(value.x, value.y, value.width, value.height);// 开始位置
		f.setVisible(true);
		new Thread() {
			@Override
			public void run() {
				while (value.x > AimX || value.y > AimY || value.width < AimWidth || value.height < AimHeight) {

					value.x -= (sped.x += a);
					value.y -= (sped.y += a);
					value.width += (sped.width += a);
					value.height += (sped.height += a);

					value.x = value.x < AimX ? AimX : value.x;
					value.y = value.y < AimY ? AimY : value.y;
					value.width = value.width > AimWidth ? AimWidth : value.width;
					value.height = value.height > AimHeight ? AimHeight : value.height;

					final Rectangle sp = new Rectangle(value.x, value.y, value.width, value.height);

					java.awt.EventQueue.invokeLater(new Runnable() {
						@Override
						public void run() {
							f.setBounds(sp.x, sp.y, sp.width, sp.height);
							f.repaint();
							if (f.isUndecorated()) {
								com.sun.awt.AWTUtilities.setWindowOpacity(f, (float) sp.width / AimWidth);// --窗口透明度设置
							}
							// System.out.println((float)sp.width/AimWidth);
						}
					});
					try {
						Thread.sleep(10);
					} catch (Exception e) {
					}
					// do{
					// }while(f.getLocation().x!=value.x||f.getLocation().y!=value
					// .y||f.getSize().width!=value.width||f.getHeight()!=value.
					// height);
				}

			}
		}.start();
	}
	public static WindowListener getOpacityWindowsListener() {
		if (OpacityWindowsListener == null) {
			OpacityWindowsListener = new WindowListener() {

				@Override
				public void windowActivated(WindowEvent e) {

				}

				@Override
				public void windowClosed(WindowEvent e) {

				}

				@Override
				public void windowClosing(final WindowEvent e) {

					stopWindow((JFrame) e.getSource());

				}

				@Override
				public void windowDeactivated(WindowEvent e) {

				}

				@Override
				public void windowDeiconified(WindowEvent e) {

				}

				@Override
				public void windowIconified(WindowEvent e) {

				}

				@Override
				public void windowOpened(final WindowEvent e) {

					PalyWindow((JFrame) e.getSource());
				}

			};
		}
		return OpacityWindowsListener;
	}

	public static WindowListener getWindowListener() {
		if (sw == null) {
			sw = new WindowListener() {

				@Override
				public void windowActivated(WindowEvent e) {

				}

				@Override
				public void windowClosed(WindowEvent e) {

				}

				@Override
				public void windowClosing(WindowEvent e) {

					ConcealWindow((JFrame) e.getSource(), 1);
				}

				@Override
				public void windowDeactivated(WindowEvent e) {

				}

				@Override
				public void windowDeiconified(WindowEvent e) {

				}

				@Override
				public void windowIconified(WindowEvent e) {

				}

				@Override
				public void windowOpened(final WindowEvent e) {

					// new Thread(new Runnable(){
					//
					// public void run() {
					// ShowWindow.DisplayWindow((JFrame)e.getSource(),6);
					// }
					// }).start();
				}
			};
		}
		return sw;
	}

	public static void main(String[] args) {
		new ShowWindow(Toolkit.getDefaultToolkit().createImage("Skin/downExited.png")).start();
	}

	public static void PalyWindow(final JFrame jf) {
		if (jf.isUndecorated()) {
			com.sun.awt.AWTUtilities.setWindowOpacity(jf, 0.01f);// --窗口透明度设置
		}
		jf.setVisible(true);
		new Thread(new Runnable() {

			@Override
			public void run() {
				int k = 18;
				for (int i = 1; i <= 80; i++) {
					try {
						if (jf.isUndecorated()) {
							com.sun.awt.AWTUtilities.setWindowOpacity(jf, i / 80f);// 窗口透明度设置
						}
						Thread.sleep(k > 0 ? k-- : 1);
					} catch (Exception ee) {
					}
				}

			}
		}).start();
	}

	public static void setWindowICO(JFrame jf) {
		jf.setIconImage(IMG.ico);
	}

	public static void stopWindow(final JFrame jf) {
		for (int i = 80; i > 0; i--) {
			try {
				if (jf.isUndecorated()) {
					com.sun.awt.AWTUtilities.setWindowOpacity(jf, i / 80f);// 窗口透明度设置
				}
				Thread.sleep(16);
			} catch (Exception es) {
			}
		}
		jf.setVisible(false);
	}

	protected boolean doing;

	protected int exists;

	public PicPanel mp;

	private static WindowListener OpacityWindowsListener;

	public ShowWindow() {
		setSize(200, 150);
		setAlwaysOnTop(true);
		setUndecorated(true);
		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
	}

	public ShowWindow(Image backgroupImage) {
		this();
		mp = new PicPanel(backgroupImage);
		optionPanel();
	}

	public ShowWindow(Image backgroupImage, Image backgroupImage2) {
		this();
		mp = new PicPanel(backgroupImage, backgroupImage2);
		optionPanel();
	}

	public void Close() {

		new Thread(new Runnable() {
			@Override
			public void run() {
				doing = true;
				int Y = (int) (getLocation().y + getSize().getHeight());// 终点顶距

				while (getLocation().y < Y) {
					setLocation(new Point(getLocation().x, getLocation().y + 10));
					float f = (Y - getLocation().y) / (float) (getSize().height);
					f = f > 1 ? 1 : f < 0 ? 0 : f;
					// System.out.println((Y-getLocation().y)+"/"+getSize().height
					// );
					if (ShowWindow.this.isUndecorated()) {
						com.sun.awt.AWTUtilities.setWindowOpacity(ShowWindow.this, f);// --窗口透明度设置
					}
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
					}
				}
				ShowWindow.this.setVisible(false);
				ShowWindow.this.dispose();
				doing = false;
			}
		}).start();
	}

	/**
	 * 取窗口中心坐标
	 * 
	 * @param w
	 * @return
	 */
	public Dimension getCenter(Window w) {
		return new Dimension(w.getLocation().x + w.getSize().width / 2, w.getLocation().y + w.getSize().height / 2);
	}

	@Override
	public void mouseClicked(MouseEvent me) {

	}

	@Override
	public void mouseEntered(MouseEvent arg0) {

		this.mp.actionEvent(true);

		exists = -1;// 鼠标进入线程暂停

	}

	@Override
	public void mouseExited(MouseEvent arg0) {

		this.mp.actionEvent(false);

		exists = 0;// 鼠标离开开始计时
	}

	@Override
	public void mousePressed(MouseEvent me) {

		// this.setVisible(false);

	}

	@Override
	public void mouseReleased(MouseEvent arg0) {

		// 关闭效果下滑，不直接消失。
		if (!doing)
			Close();

	}

	private void optionPanel() {
		mp.setBorder(new LineBorder(Color.BLACK));
		this.setContentPane(mp);
		mp.addMouseListener(this);
	}

	public void start() {
		double width = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
		double height = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
		int x = (int) ((width - getSize().width));
		int y = (int) ((height) - 30);
		setLocation(x, y);
		this.setVisible(true);
		if (this.isUndecorated()) {
			com.sun.awt.AWTUtilities.setWindowOpacity(this, 0.1f);// --
		}
		// 窗口透明度设置
		new Thread(new Runnable() {
			@Override
			public void run() {
				doing = true;
				int Y = (int) (getLocation().y - getSize().getHeight());
				try {
					Thread.sleep(500);// 浮出前停顿
				} catch (InterruptedException e1) {
					e1.printStackTrace();
				}
				while (getLocation().y > Y) {
					setLocation(new Point(getLocation().x, getLocation().y - 10));
					float f = (getSize().height - (getLocation().y - Y)) / (float) (getSize().height);
					f = f > 1 ? 1 : f < 0 ? 0 : f;
					// System.out.println((getLocation().y-Y)+"/"+getSize().height
					// );
					if (ShowWindow.this.isUndecorated()) {
						com.sun.awt.AWTUtilities.setWindowOpacity(ShowWindow.this, f);// --窗口透明度设置
					}
					try {
						Thread.sleep(20);
					} catch (InterruptedException e) {
					}
				}

				// ===========3秒后自动关闭============

				exists = 0;
				new Thread(new Runnable() {

					@Override
					public void run() {

						while (exists == -1 || (exists++) < 3)
							try {
								Thread.sleep(1000);
							} catch (InterruptedException e) {
							}
						if (ShowWindow.this.isVisible())
							Close();
					}
				}).start();// 启动时钟关闭窗口线程
				doing = false;
			}
		}).start();
	}
}
